<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shipping extends MX_Controller {


	function __construct(){

		parent::__construct();

        $this->load->library('form_validation');

		//$this->load->library('auth/tank_auth');
		if (!Modules::run('auth/is_login'))
			redirect('/auth/login/manager');

		$this->load->model('manager_model');
	}

	function validation($product_id = ''){

		$this->form_validation->set_error_delimiters('<p class="error-form">', '</p>');
		$this->form_validation->set_message('required', ' %s is required ');

		if (!empty($product_id)){
			$this->form_validation->set_rules('product_id', 'Product ID', 'trim|xss_clean|required');
		}

		$this->form_validation->set_rules('shipping_method', 'Shipping Method', 'trim|xss_clean|required');
		$this->form_validation->set_rules('area', 'Area', 'trim|xss_clean|required');
		$this->form_validation->set_rules('cost', 'Cost', 'trim|xss_clean|required');


		if ($this->form_validation->run() == TRUE){

				if(!$this->cek_price_value($this->form_validation->set_value('cost'))) {
					$message = '<p class="error-form">Please input a valid cost!</p>';
					echo $message;
					exit();
				}
				else{
					
					$data_save['shipping_method'] = $this->form_validation->set_value('shipping_method');
					$data_save['location'] 	= $this->form_validation->set_value('area');
					$data_save['price'] 	= $this->form_validation->set_value('cost');
					$data_save['date_add'] 	= date('Y-m-d H:i:s');

					if (!empty($product_id)) {

						$data_save['product_id'] = $this->form_validation->set_value('product_id');

						$id = $this->manager_model->save_product_shipping($data_save, true);
					}
					else {
						
						$id = md5($data_save['location'].date('His'));

						$data_shipping = $this->session->userdata('shipping');

						if(!empty($data_shipping) && is_array($data_shipping)) {							

							foreach ($data_shipping as $key => $val) {
								$shipping[$key] = $val;
							}

							$shipping[$id] = $data_save;

							$this->session->unset_userdata('shipping');
							$this->session->set_userdata('shipping', $shipping);

						}
						else {							

							$new_shipping[$id] = $data_save;

							$this->session->set_userdata('shipping', $new_shipping);
						}
					}

					$msg = array('message' => 'success', 'id'=>$id);
					echo json_encode($msg);
				}
		}

		$error = validation_errors();

		echo $error;

	}

	function delete_shipping_session(){

		$id = $this->input->post('shipping_id', true);

		$data_shipping = $this->session->userdata('shipping');

		$shipping = false;

		if(!empty($data_shipping) && is_array($data_shipping)) {							

			foreach ($data_shipping as $key => $val) {

				if ($key != $id)
				$shipping[$key] = $val;
			}

			$this->session->unset_userdata('shipping');
			$this->session->set_userdata('shipping', $shipping);

		}

		echo 'success';
	}

	function cek(){
		//$this->session->set_userdata('shipping', $new_shipping);
		$data_shipping = $this->session->userdata('shipping');
		var_dump($data_shipping);
	}

	function cek_price_value($val){

		if(!preg_match('/^[0-9]+.[0-9]{0,4}$/', $val))
		{
			$this->form_validation->set_message('price', 'The %s field is not valid!');
			return FALSE;
		}
		else
		{
			$value = $val + 0 ;
			if($value == 0){
				$this->form_validation->set_message('price', 'The %s field is not Valid!');
				return FALSE;
			}
			else{
				return TRUE;
			}
			
		}
	}

	function delete(){
		$product_id = $this->input->post('product_id');
		$shipping_id = $this->input->post('shipping_id');
		$this->manager_model->delete_product_shipping($shipping_id,$product_id);
		echo 'success';
	}

	function get_area(){
		$query = $this->input->get('query', true);
		$data_area = $this->manager_model->get_shipping_area($query);

		$area = array();

		foreach ($data_area as $key => $val) {
			$area[] = $val['name'];
		}

		$json_area = array('options' => $area);

		header('Content-type: application/json');
		echo json_encode($json_area);
	}

}